package com.ljx.recruitment.interview.service.impl;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import com.ljx.recruitment.common.exception.ApiException;
import com.ljx.recruitment.interview.domain.dto.DashboardDeliverTrendDateDto;
import com.ljx.recruitment.interview.domain.vo.resp.DashboardDeliverTrendResp;
import com.ljx.recruitment.interview.domain.vo.resp.DashboardDetailResp;
import com.ljx.recruitment.interview.domain.vo.resp.DashboardJobCategoryResp;
import com.ljx.recruitment.interview.enums.DashboardDeliverTrendTimeEnum;
import com.ljx.recruitment.interview.service.DashboardService;
import com.ljx.recruitment.interview.service.DeliverService;
import com.ljx.recruitment.interview.service.JobService;
import com.ljx.recruitment.security.util.UserUtil;
import com.ljx.recruitment.user.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

/**
 * @Author: LiJunXi
 * @Description:
 * @Date: 2025-04-04  15:53
 */
@Service
public class DashboardServiceImpl implements DashboardService {

    @Autowired
    private UserService userService;

    @Autowired
    private DeliverService deliverService;

    @Autowired
    private JobService jobService;

    @Override
    public DashboardDetailResp getInfo() {
        Integer userCount = userService.getTenantUserCount(UserUtil.tryToGetTenantId());
        Integer deliverCount = deliverService.getTenantDeliverCount(UserUtil.tryToGetTenantId());
        Integer jobCount = jobService.getTenantJobCount(UserUtil.tryToGetTenantId());
        Integer rate = deliverService.getDeliverCompletionRate();

        return new DashboardDetailResp(userCount, jobCount, deliverCount, rate);
    }

    @Override
    public DashboardDeliverTrendResp getDeliverTrend(String time) {
        if (StrUtil.isEmpty(time)) {
            throw new ApiException("时间参数不能为空");
        }
        List<Date> dates = new ArrayList<>();
        // 去掉时分秒
        Date date =  DateUtil.beginOfDay(new Date());
        int days = DashboardDeliverTrendTimeEnum.getValueByKey(time);
        for(int i = 0; i < days; i++) {
            dates.add(DateUtil.offsetDay(date, -1 * i));
        }

        Map<Date, Integer> groupDateMap = deliverService.getTenantCountGroupByDate(
                UserUtil.tryToGetTenantId(), dates)
                .stream()
                .map(item -> new DashboardDeliverTrendDateDto(DateUtil.beginOfDay(item.getDate()), item.getCount()))
                .collect(Collectors.toMap(DashboardDeliverTrendDateDto::getDate, DashboardDeliverTrendDateDto::getCount));

        List<String> xData = new ArrayList<>();
        List<Integer> yData = new ArrayList<>();
        dates.forEach(item -> {
            xData.add(DateUtil.formatDate(item));
            yData.add(groupDateMap.getOrDefault(item, 0));
        });

        return new DashboardDeliverTrendResp(xData, yData);
    }

    @Override
    public List<DashboardJobCategoryResp> getJobCategory() {
        return jobService.getTenantJobCategory(UserUtil.tryToGetTenantId());
    }
}
